﻿#define ISSERVER
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Controllers
{

#if ISSERVER
    public class ServerController : ControllerBase 
    {
        public ActionResult Version(string item)
        {
            return View();
        }

        public ActionResult Message(string item)
        {
            int id = int.Parse(Config.SiteConfig["MessageID"]);
            int page = 1;
            int size = 10;
            string func = "ServerMessage";
            if (!string.IsNullOrEmpty(item))
            {
                string[] argument = item.Split('-'); ;
                if (argument.Length == 3)
                {
                    try
                    {
                        page = int.Parse(argument[0]);
                        size = int.Parse(argument[1]);
                        if (page < 1)
                            page = 1;
                        if (size > 50)
                            size = 50;
                        func = argument[2];
                    }
                    catch
                    {

                    }
                }
            }
            
            var items = JinianNet.JNCms.Data.DataAccess.CreateArticle().GetList(id, page, size);
            //var items = JinianNet.JNCms.Data.DataAccess.CreateArticle().GetList(id, page, size).ConvertAll<object>(m => new { Title=m.Title,Url=m.IsUrl ? m.Url :   JinianNet.Common.Url.Concat("http://",Config.Site.Host,JinianNet.Web.HttpRunInfo.SiteVirtualPath,"Article",m.KeyName)   });
            string[] list = new string[items.Count];
            for (int i=0;i<items.Count;i++)
            {
                list[i] = string.Concat("{\"Title\":\"", items[i].Title.Replace("\"", ""), "\",\"Date\":\"",items[i].CreateTime.ToString("yyyy-MM-dd"), "\",\"Url\":\"", items[i].IsUrl ? items[i].Url : JinianNet.Common.Url.Concat("http://", Config.Site.Host, JinianNet.Web.HttpRunInfo.SiteVirtualPath, "Article", items[i].KeyName), "\"}");
            }
            return Content(string.Concat(func,"([",string.Join(",",list),"])")); //Json(items, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Template(int page,int size)
        {
            StringBuilder sbr = new StringBuilder();
            sbr.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Template Url=\"");
            sbr.Append(JinianNet.Common.Url.Concat("http://", Config.Site.Host, Config.Site.SiteDirectory, Config.Site.TemplateDirectory, "/"));
            sbr.Append("\">");
            if (size > 0)
            {
                if (size > 50)
                {
                    size = 50;
                }
                var list = Data.DataAccess.CreateTemplate().GetList(page, size);
                foreach (var item in list)
                {
                    sbr.Append("<List><ID>");
                    sbr.Append(item.ID.ToString());
                    sbr.Append("</ID>");
                    sbr.Append("<Key><![CDATA[");
                    sbr.Append(item.Key);
                    sbr.Append("]]></Key>");
                    sbr.Append("<Title><![CDATA[");
                    sbr.Append(item.Title);
                    sbr.Append("]]></Title>");
                    sbr.Append("<Author><![CDATA[");
                    sbr.Append(item.Author);
                    sbr.Append("]]></Author>");
                    sbr.Append("<Version><![CDATA[");
                    sbr.Append(item.Version);
                    sbr.Append("]]></Version>");
                    sbr.Append("<Url><![CDATA[");
                    sbr.Append(item.Url);
                    sbr.Append("]]></Url>");
                    sbr.Append("<SystemVersion><![CDATA[");
                    sbr.Append(item.SystemVersion);
                    sbr.Append("]]></SystemVersion>");
                    sbr.Append("<Description><![CDATA[");
                    sbr.Append(item.Description);
                    sbr.Append("]]></Description>");
                    sbr.Append("<CreateTime><![CDATA[");
                    sbr.Append(item.CreateTime.ToString("yyy-MM-dd"));
                    sbr.Append("]]></CreateTime></List>");
                }
            }
            sbr.Append("</Template>");
            return Content(sbr.ToString());
        }
    }
#endif

}
